package net.maku.association.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.association.dao.InterestAssociationDao;
import net.maku.association.dao.InterestAssociationMemberDao;
import net.maku.association.entity.InterestAssociationEntity;
import net.maku.association.entity.InterestAssociationMemberEntity;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.association.convert.InterestAssociationApplyConvert;
import net.maku.association.entity.InterestAssociationApplyEntity;
import net.maku.association.query.InterestAssociationApplyQuery;
import net.maku.association.vo.InterestAssociationApplyVO;
import net.maku.association.dao.InterestAssociationApplyDao;
import net.maku.association.service.InterestAssociationApplyService;
import net.maku.member.dao.MemberUserDao;
import net.maku.member.entity.MemberUserEntity;
import net.maku.system.dao.SysOrgDao;
import net.maku.system.entity.SysOrgEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 申请加入协会
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-04-26
 */
@Service
@AllArgsConstructor
public class InterestAssociationApplyServiceImpl extends BaseServiceImpl<InterestAssociationApplyDao, InterestAssociationApplyEntity> implements InterestAssociationApplyService {


    private final InterestAssociationMemberDao interestAssociationMemberDao;
    private final MemberUserDao memberUserDao;
    private final SysOrgDao sysOrgDao;

    private final InterestAssociationDao interestAssociationDao;
    @Override
    public PageResult<InterestAssociationApplyVO> page(InterestAssociationApplyQuery query) {
        IPage<InterestAssociationApplyEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(InterestAssociationApplyConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<InterestAssociationApplyEntity> getWrapper(InterestAssociationApplyQuery query){
        LambdaQueryWrapper<InterestAssociationApplyEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(InterestAssociationApplyVO vo) {
        InterestAssociationApplyEntity entity = InterestAssociationApplyConvert.INSTANCE.convert(vo);
        InterestAssociationEntity interestAssociationEntity = interestAssociationDao.selectById(vo.getAssociationId());
        entity.setAssociationName(interestAssociationEntity.getName());
        baseMapper.insert(entity);
    }

    @Override
    public void update(InterestAssociationApplyVO vo) {
        InterestAssociationApplyEntity entity = InterestAssociationApplyConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(InterestAssociationApplyVO vo) {
        InterestAssociationApplyEntity entity = InterestAssociationApplyConvert.INSTANCE.convert(vo);
        Integer auditStatus = entity.getAuditStatus();
        if(auditStatus == 1){
            InterestAssociationApplyEntity apply = baseMapper.selectById(vo.getId());
            MemberUserEntity userEntity = memberUserDao.selectById(apply.getUserId());
            SysOrgEntity orgEntity = sysOrgDao.selectById(userEntity.getOrgId());
            InterestAssociationMemberEntity add = new InterestAssociationMemberEntity();
            add.setAssociationId(apply.getAssociationId());
            add.setJoinTime(new Date());
            add.setUserId(apply.getUserId());
            add.setOrgId(userEntity.getOrgId());
            add.setRealName(userEntity.getRealName());
            add.setOrgName(orgEntity.getUnionAbbreviation());
            interestAssociationMemberDao.insert(add);
        }
        updateById(entity);
    }

}